import Fluent
import FluentMySQLDriver
import NIOSSL
import Vapor
import Queues
import QueuesRedisDriver

// configures your application
public func configure(_ app: Application) async throws {
    var tlsConfig = TLSConfiguration.makeClientConfiguration()
    tlsConfig.certificateVerification = .none
    app.http.client.configuration.tlsConfiguration = tlsConfig
    app.http.server.configuration.hostname = "0.0.0.0"

    // MARK: 数据库配置
    app.databases.use(
        DatabaseConfigurationFactory.mysql(
            hostname: "localhost",  // MySQL 运行在同一服务器上
            port: 3306,             // MySQL 默认端口号
            username: "KEN",        // 数据库用户名
            password: "000",        // 数据库密码
            database: "testdb",      // 数据库名称
            tlsConfiguration: tlsConfig
        ), as: .mysql)

    // MARK: 添加数据迁移
    app.migrations.add(UserModelMigration())
    app.migrations.add(TokenModelMigration())
    app.migrations.add(CourseModelMigration())
    app.migrations.add(SessionModelMigration())
    app.migrations.add(GuideModelMigration())
    app.migrations.add(ArticleModelMigration())
    
    app.migrations.add(CreateUserSeed())
    
    // MARK: Redis 设置
    try app.queues.use(.redis(url: "redis://localhost:6379/0"))
    
    // MARK: Job 设置
    let publishJob = PublishJob()
    app.queues.schedule(publishJob).everySecond()
    try app.queues.startScheduledJobs()
    // 注册路由
    try await app.autoMigrate()
    try routes(app)
}
